//
//  AppDelegate.m
//  xiaorizi
//
//  Created by kun on 15/10/23.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "AppDelegate.h"
#import "WeiboUser.h"

#import "WBHttpRequest+WeiboUser.h"
#import "WXApiManager.h"
#import "KZLoginViewController.h"
#import "KZMineTableViewContriller.h"

@interface AppDelegate ()<WeiboSDKDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:kAppKey];
    
    //三方登陆注册微信
    [WXApi registerApp:WECHAT_APP_ID withDescription:@"2.0"];
    
    
    //三方登陆QQ
   

    
    return YES;
}


/**
 收到一个来自微博客户端程序的请求
 
 收到微博的请求后，第三方应用应该按照请求类型进行处理，处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
 @param request 具体的请求对象
 */
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{


}

/**
 收到一个来自微博客户端程序的响应
 
 收到微博的响应后，第三方应用可以通过响应类型、响应的数据和 WBBaseResponse.userInfo 中的数据完成自己的功能
 @param response 具体的响应对象
 */
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    
    
    NSString *title = NSLocalizedString(@"认证结果", nil);
   if(response)
   {
    NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.userId: %@\nresponse.accessToken: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken],  NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                          otherButtonTitles:nil];
    
    self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
    self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
    self.wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken];
    
    [WBHttpRequest requestForUserProfile:self.wbCurrentUserID withAccessToken:self.wbtoken andOtherProperties:nil queue:nil withCompletionHandler:^(WBHttpRequest *httpRequest, id result, NSError *error) {
        DemoRequestHanlder(httpRequest, result, error);
    }] ;
    
   }
    //2 微信三方登陆请求
    
    
}

void DemoRequestHanlder(WBHttpRequest *httpRequest, id result, NSError *error)
{
    NSString *title = nil;
    UIAlertView *alert = nil;
    
    if (error)
    {
        title = NSLocalizedString(@"请求异常", nil);
        alert = [[UIAlertView alloc] initWithTitle:title
                                           message:[NSString stringWithFormat:@"%@",error]
                                          delegate:nil
                                 cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                 otherButtonTitles:nil];
    }
    else
    {
        title = NSLocalizedString(@"收到网络回调", nil);
        
        WeiboUser *userInfo = result;
        //数据写入本地沙盒中
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:@(YES) forKey:@"isLogin"];
        [user setObject:userInfo.name forKey:@"LoginName"];
        [user setObject:userInfo.profileImageUrl forKey:@"LoginImg"];
        
#warning 这里应该实现一个通知回调!!!
        
        
    }
    
}




- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    
    NSLog(@"openURL  %@",[url path]);
    NSLog(@"sourceApplication %@",sourceApplication);
    NSLog(@"annotation  %@",annotation);
    
    
    if ([sourceApplication isEqualToString:@"com.tencent.xin"]) {
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    }
    
    if ([sourceApplication isEqualToString:@"com.sina.weibo"]) {
        return [WeiboSDK handleOpenURL:url delegate:self];
    }
    
    if([sourceApplication isEqualToString:@""])
    {
        [TencentOAuth HandleOpenURL:url];
    }
    
    
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    NSLog(@" handleOpenURL  %@",[url path]);
    
    NSString *string =[url absoluteString];
    if ([string hasPrefix:@"WB"] || [string hasPrefix:@"wb"])
    {
        return [WeiboSDK handleOpenURL:url delegate:self];
    }
    else if ([string hasPrefix:@"WX"] || [string hasPrefix:@"wx"])
    {
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    }
    else
    {
        [TencentOAuth HandleOpenURL:url];
    }

    return  YES;
}



@end
